package inits

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"zy5/global"
	"zy5/internal/model"
)

func MysqlInit() {
	var err error
	dsn := "root:root@tcp(127.0.0.1:3306)/zy5?charset=utf8mb4&parseTime=True&loc=Local"
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Println("数据库链接失败")
		panic(err)
	}
	log.Println("数据库链接成功")
	err = global.DB.AutoMigrate(&model.User{}, &model.Shop{}, &model.Like{}, &model.Comment{})
	if err != nil {
		log.Println("自动建表失败")
		return
	}
	log.Println("自动迁移建表成功")
}
